home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / fortify.lha / zfortify.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-03  |  3.4 KB  |  84 lines

  1. // zfortify.hpp - all user code that wan't to be fully fortified should
  2. //                include this file with ZFORTIFY defined
  3. #ifndef ZFORTIFY_H
  4. #define ZFORTIFY_H
  5.  
  6. #include <new.h>
  7.  
  8. // Our wonderful new operators
  9. void *operator new(size_t size);
  10. void *operator new(size_t size, char *file, unsigned long line);
  11. void  operator delete(void *pointer);
  12.  
  13. // Some compilers use a different new operator for newing arrays. 
  14. // This includes GNU G++ (2.6.0) and Borland C++ (4.02)
  15. #ifdef ZFORTIFY_PROVIDE_ARRAY_NEW
  16. void *operator new[](size_t size);
  17. void *operator new[](size_t size, char *file, unsigned long line);
  18. #endif
  19.  
  20. // Prototypes - for the compiler's benefit only. The user should never
  21. // need to call these functions with these arguments. Use the macros
  22. // defined a little further down in the file.
  23.  
  24. int           ZFortify_EnterScope(char *file, unsigned long line);
  25. int           ZFortify_LeaveScope(char *file, unsigned long line);
  26. int           ZFortify_OutputAllMemory(char *file, unsigned long line);
  27. int           ZFortify_DumpAllMemory(int scope, char *file, unsigned long line);
  28. int           ZFortify_CheckAllMemory(char *file, unsigned long line);
  29. int           ZFortify_CheckPointer(void *uptr, char *file, unsigned long line);
  30. int           ZFortify_Disable(char *file, unsigned long line);
  31. int           ZFortify_PreDelete(char *file, unsigned long line);
  32.  
  33.  
  34. typedef void (*ZFortify_OutputFuncPtr)(const char *);
  35. ZFortify_OutputFuncPtr ZFortify_SetOutputFunc(ZFortify_OutputFuncPtr Output);
  36.  
  37. int           ZFortify_SetNewFailRate(int Percent);
  38.  
  39. #ifndef ZFORTIFY_CPP /* Only define the macros if we're NOT in ZFortify.cpp */
  40.  
  41. #ifdef ZFORTIFY /* Add file and line information to the ZFortify calls */
  42.  
  43. // Add  soucecode information to the new and delete calls.  Note that these
  44. // macros  will  create  syntax  errors  when a piece of code is defining a
  45. // custom  new or delete operator.  If this happens, you will need to place
  46. // #undef's and #define's around the offending code (sorry).
  47. //
  48. // eg.
  49. // #undef new 
  50. // void *X::operator new(size_t) { return malloc(size_t); }
  51. // #define new ZFortify_New
  52. //
  53. #define ZFortify_New                    new(__FILE__, __LINE__)
  54. #define ZFortify_Delete                 ZFortify_PreDelete(__FILE__, __LINE__), delete
  55. #define new                             ZFortify_New
  56. #define delete                          ZFortify_Delete
  57.  
  58. #define ZFortify_EnterScope()           ZFortify_EnterScope(__FILE__, __LINE__)
  59. #define ZFortify_LeaveScope()           ZFortify_LeaveScope(__FILE__, __LINE__)
  60. #define ZFortify_OutputAllMemory()      ZFortify_OutputAllMemory(__FILE__, __LINE__)
  61. #define ZFortify_DumpAllMemory(s)       ZFortify_DumpAllMemory(s, __FILE__, __LINE__)
  62. #define ZFortify_CheckAllMemory()       ZFortify_CheckAllMemory(__FILE__, __LINE__)
  63. #define ZFortify_CheckPointer(ptr)      ZFortify_CheckPointer(ptr, __FILE__, __LINE__)
  64. #define ZFortify_Disable()              ZFortify_Disable(__FILE__, __LINE__)
  65.  
  66. #else // Define the special ZFortify functions away to nothing
  67.  
  68. #define ZFortify_OutputAllMemory()      0
  69. #define ZFortify_DumpAllMemory(s)       0
  70. #define ZFortify_CheckAllMemory()       0
  71. #define ZFortify_CheckPointer(ptr)      1
  72. #define ZFortify_Disable()              1
  73. #define ZFortify_SetOutputFunc()        0
  74. #define ZFortify_SetMallocFailRate(p)   0
  75.  
  76. #define ZFortify_New                    new
  77. #define ZFortify_Delete                 delete
  78.  
  79. #endif // ZFORTIFY
  80.  
  81. #endif // ZFORTIFY_CPP
  82.  
  83. #endif // ZFORTIFY_H
  84.